package com.example.smartcity_poverty.ui.AllService.LogisticsQuery.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import com.example.smartcity_poverty.App
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_lq_show.*
import kotlinx.android.synthetic.main.activity_lq_tou_su_add.*
import kotlinx.coroutines.Dispatchers
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject

/**
 * 新增投诉
 */
class LqTouSuAddActivity : AppCompatActivity() {
    // 接收单号
    private var no: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_lq_tou_su_add)
        title = "投诉"
        no = intent.getStringExtra("id")


        // 填充数据
        runThread()
    }

    private fun runThread() = coroutine(Dispatchers.Main) {
        setShowList()
    }


    /**
     * 公司信息
     */
    private suspend fun setShowList() {
        api.getLqNoQuery(getToken(), no!!).apply {
            if (this.code == 200) {
                runOnUiThread {
                    this.data?.let {
                        lqTouSuTv1.text = "公司名称: ${it.company?.name}"
                        lqTouSuTv2.text = "运单号: ${it.infoNo}"


                        val comId = it.companyId
                        val comNo = it.infoNo
                        // 点击进行投诉
                        lqTouSuOk.setOnClickListener {
                            if (lqTouSuEd1.text.toString()
                                    .isNotEmpty() && lqTouSuEd2.text.toString().isNotEmpty()
                            ) {
                                coroutine(Dispatchers.Main) {
                                    api.getLqTouSu(
                                        getToken(), JSONObject().apply {
                                            put("companyId", comId)
                                            put("infoNo", comNo)
                                            put("questionType", 1)
                                            put("description", lqTouSuEd2.text.toString())
                                        }.toString()
                                            .toRequestBody("application/json".toMediaTypeOrNull())
                                    ).apply {
                                        if (this.code == "200") {
                                            runOnUiThread {
                                                this.msg.show()
                                                finish()
                                            }
                                        } else runOnUiThread { this.msg.show() }
                                    }
                                }
                            } else runOnUiThread { "不能为空！".show() }
                        }
                    }
                }
            } else runOnUiThread { this.msg?.show() }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}